﻿//using System;
//using System.Web.Mvc;
//using AutoMapper;
//using NC.Data;
//using NC.ViewModels;
//using System.Text.RegularExpressions;
//using NC.Models;

//namespace NC.Controllers
//{
//    public class AccountController : Controller
//    {
//        //AccountUserService _accountUserService;
//        private readonly AccountUserRepository _accountUserRepository;
//        public AccountController()
//        {
//            //_accountUserService = new AccountUserService();
//            _accountUserRepository = new AccountUserRepository();
//        }
//        /// <summary>
//        /// This method returns RegisterAccount view where the user can provide initial registration information
//        /// </summary>
//        /// <returns>Register account view</returns>
//        public ActionResult RegisterAccount()
//        {
//            return View("RegisterAccount");
//        }

//        /// <summary>
//        /// Post the account user to this method to register account
//        /// </summary>
//        /// <param name="accountUser">Account user that will be registered</param>
//        [HttpPost]
//        public ActionResult RegisterAccount(NC.Models.AccountUserModel accountUser)
//        {
//            accountUser = PopulateAccountUser(accountUser);
//            accountUser.AddressId = NcConstants.DefaultAddressId;
//            if (ModelState.IsValid)
//            {
//                if (_accountUserRepository.IsDuplicateEmail(accountUser.Email))
//                {
//                    ViewBag.Error = NcMessage.EmailAlreadyExists;
//                }
//                //To do: Check for valid email id
//                //else if (is valid email)
//                //{

//                //}
//                //To do: Check for valid password rule
//                //else if (follows password rule)
//                //{

//                //}
//                else
//                {
//                    accountUser.GenerateActivationCode();
//                    var dataAccountUser = Mapper.Map<NC.Data.AccountUser>(accountUser);
//                    var user = Mapper.Map<NC.Models.AccountUserModel>(_accountUserRepository.RegisterAccountUser(dataAccountUser));
//                    var mailMessage = user.GetUserActivationMessage();
//                    var email = new Email(mailMessage);
//                    email.Send();
//                    ViewBag.SuccessMessage = NcMessage.EmailSentNotification;
//                }
//            }
//            return View("RegisterAccount");
//        }

//        public NC.Models.AccountUserModel PopulateAccountUser(NC.Models.AccountUserModel accountUser)
//        {
//            accountUser.FirstName = accountUser.FirstName ?? "";
//            accountUser.MiddleName = accountUser.FirstName == null ? "" : accountUser.MiddleName;
//            accountUser.LastName = accountUser.LastName ?? "";
//            accountUser.Sex = accountUser.Sex ?? "";
//            accountUser.DOB = NcConstants.DefaultDOB;
//            return accountUser;
//        }

//        /// <summary>
//        /// This method activates user with the given activation code and redirects to GetLogInPage
//        /// </summary>
//        /// <param name="ac">Activation code for user who is going to be activated</param>
//        /// <returns>Redirect to loginpage or error page</returns>
//        public ActionResult ActivateUser(string activationCode)
//        {
//            var dataAccountUser = _accountUserRepository.GetAccountUserByActivationCode(activationCode);
//            if (dataAccountUser != null && !dataAccountUser.Active)
//            {
//                if (_accountUserRepository.ActivateUser(dataAccountUser.Id))
//                {
//                    ViewBag.SuccessActivation = NcMessage.Acitvated;
//                }
//                return RedirectToAction("GetLogInPage", "LogIn");
//            }
//            ViewBag.NotActivated = NcMessage.AcitvateError;
//            return RedirectToAction("GetLogInPage", "LogIn");
//        }

//        [HttpGet]
//        public ActionResult ResetPasswordCode(string resetcode, int userid)
//        {
//            resetcode = Request.QueryString["code"];
//            userid = Convert.ToInt32(Request.QueryString["userid"]);
//            var resetuser = _accountUserRepository.GetAccountUserByResetPasswordCode(resetcode);
//            if (resetuser != null)
//            {
//                if (_accountUserRepository.SaveResetPasswordCode(resetcode, userid))
//                {
//                    return RedirectToAction("ResetPassword", new { userid = userid });
//                }
//            }
//            else
//                ViewBag.ResetError = NcMessage.NotAcitvated;
//            return View("Error");
//        }

//        [HttpGet]
//        public ActionResult ResetPassword()
//        {
//            TempData["userdata"] = Convert.ToInt32(Request.QueryString["userid"]);
//            return View("VerifyEmail");
//        }

//        public ActionResult FullRegistration()
//        {
//            return PartialView();
//        }

//        /// <summary>
//        /// Post the account user registration details to this method to register other details like fullname and location
//        /// </summary>
//        /// <param name="accountUser"></param>
//        /// <returns></returns>
//        [HttpPost]
//        public ActionResult RegisterUserAccount(NC.Data.AccountUser accountUser)
//        {
//            return View("Index");
//        }

//        [HttpPost]
//        public ActionResult ResetPasswordByEmail(UserLogin users)
//        {
//            if (ModelState.IsValid)
//            {
//                var result = _accountUserRepository.GetAccountUserByEmailId(users.Email);
//                if (result != null && result.Active)
//                {
//                    var result1 = Mapper.Map<NC.Models.AccountUserModel>(result);
//                    result1.GenerateResetPasswordCode();
//                    var mailMessage = result1.GetPasswordActivationMessage();
//                    var email = new Email(mailMessage);
//                    email.Send();
//                    ViewBag.message = NcMessage.EmailSentNotification;
//                }
//                else
//                    ViewBag.Error = NcMessage.EmailNotFound + " or " + NcMessage.NotAcitvated;
//            }

//            return View("ResetPassword");
//        }

//        public NC.Data.AccountUser AutoRegisterWhileCreatingPost(NC.Models.AccountUserModel accountUser)
//        {
//            //create an account user with given info
//            PopulateAccountUser(accountUser);
//            accountUser.GenerateActivationCode();
//            accountUser.GeneratePassword();
//            var dataAccountUser = Mapper.Map<NC.Data.AccountUser>(accountUser);
//            accountUser = Mapper.Map<NC.Models.AccountUserModel>(_accountUserRepository.RegisterAccountUser(dataAccountUser));
//            var mailMessage = accountUser.GetAutoUserCreationMessage();
//            var email = new Email(mailMessage);
//            email.Send();
//            return accountUser;
//        }

//        public bool passwordcheck(string password)
//        {
//            string regex = @"^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$";
//            if (!Regex.IsMatch(password, regex))
//            {
//                return false;
//            }
//            return true;
//        }

//        [HttpPost]
//        public ActionResult UpdateResetPassword(NC.Data.AccountUser users)
//        {
//            if (ModelState.IsValid)
//            {
//                var iduser = (int)TempData["userdata"];
//                // string cpass = Request["cresetpass"];
//                var match = passwordcheck(users.Password);
//                if (match && _accountUserRepository.ResetPasswordByAccountUserId(users.Password, iduser))
//                {
//                    TempData["ResetSuccess"] = NcMessage.ResetSucceed;
//                    return RedirectToAction("AutoLogin", new { userid = iduser });

//                    //return View("UpdateResetPassword");
//                }
//                else
//                    ViewBag.Notmatch = NcMessage.InvalidPassword;
//            }
//            return View("ResetPassword");
//        }      
//    }
//}
